package com.hksj.wmp.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.base.mapper.DefaultDeviceMapper;
import com.hksj.base.query.BaseDeviceQuery;
import com.hksj.base.query.BaseSpecialityQuery;
import com.hksj.base.service.DefaultDeviceService;
import com.hksj.base.vo.DeviceStatusVO;
import com.hksj.base.vo.PropertyVO;
import com.hksj.cockpit.service.ComprehensiveCockpitService;
import com.hksj.common.core.common.PropertyCodeUnitEnum;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import com.hksj.common.core.enums.DeviceState;
import com.hksj.wmp.mapper.WeChatDeviceMapper;
import com.hksj.wmp.vo.DeviceRunStatusVO;
import com.hksj.wmp.vo.JJDLevelSituationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zengke
 * @version 1.0
 * @see WeChatDeviceService
 * @since 2024/10/14 18:01
 */

@Service
public class WeChatDeviceService {

    @Autowired
    private ComprehensiveCockpitService comprehensiveCockpitService;

    @Autowired
    private WeChatDeviceMapper wechatDeviceMapper;

    @Autowired
    private DefaultDeviceMapper defaultDeviceMapper;

    @Autowired
    private DefaultDeviceService defaultDeviceService;

    /**
     * 查询设备总体情况
     * @param query 基础专业查询类
     * @return Map集合 key:各专业的code、专业对应的设备数量
     */
    public Map<String,String> queryDeviceOverallStatus(BaseSpecialityQuery query){
        return comprehensiveCockpitService.queryDeviceOverview(query);
    }

    /**
     * 查询洁净度等级情况
     * @param query 基础专业查询类
     * @return 洁净度等级情况
     */
    public JJDLevelSituationVO queryJJDLevelSituation(BaseSpecialityQuery query) {

        //查询优的洁净度的设备数量
        String excellentNum = wechatDeviceMapper.queryJJDLevelSituation(query, 1);

        //查询良的洁净度的设备数量
        String goodNum = wechatDeviceMapper.queryJJDLevelSituation(query, 2);

        //查询差的洁净度的设备数量
        String poorNum = wechatDeviceMapper.queryJJDLevelSituation(query, 3);

        //封装洁净度等级情况的VO
        JJDLevelSituationVO jjdLevelSituationVO = new JJDLevelSituationVO();

        jjdLevelSituationVO.setExcellentNum(excellentNum);
        jjdLevelSituationVO.setGoodNum(goodNum);
        jjdLevelSituationVO.setPoorNum(poorNum);

        //返回洁净度等级情况
        return jjdLevelSituationVO;
    }

    public DeviceRunStatusVO queryDeviceRunStatus(BaseSpecialityQuery query) {

        //停止的数量
        String stopNum = wechatDeviceMapper.queryDeviceRunStatus(query,0);

        //启动数量
        String startNum = wechatDeviceMapper.queryDeviceRunStatus(query,1);

        //封装设备运行状态VO
        DeviceRunStatusVO deviceRunStatusVO = new DeviceRunStatusVO();

        deviceRunStatusVO.setStartNum(startNum);
        deviceRunStatusVO.setStopNum(stopNum);

        //返回设备运行状态
        return deviceRunStatusVO;
    }

    public Page<DeviceStatusVO> queryKTJZDeviceList(BaseDeviceQuery query) {
        Page<DeviceStatusVO> page = new Page<>(query.getPageNum(),query.getPageSize());
        List<String> deviceIdList = wechatDeviceMapper.pageDeviceId(query.getDisAblePage() ? null : page,query);
        if (!ObjectUtils.isEmpty(deviceIdList)){
            query.setDeviceIdList(deviceIdList);
            List<DeviceStatusVO> list =  defaultDeviceService.getDefaultDeviceMapper().pageDevice(query);
            defaultDeviceService.wrapAddr(list);
            for (DeviceStatusVO deviceStatusVO : list) {
                if (!Objects.equals(DeviceState.online.getValue(),deviceStatusVO.getState())){
                    if (!ObjectUtils.isEmpty(deviceStatusVO.getPropertyVOList())){
                        for (PropertyVO propertyVO : deviceStatusVO.getPropertyVOList()) {
                            propertyVO.setValue(null);
                            propertyVO.setNumberValue(null);
                            propertyVO.setAlarmStatus(null);
                        }
                    }
                }
            }
            //query.getSpecialityCode().equals(SpecialityModelCodeEnum.air_quality.getCode())
            if (Objects.equals(query.getSpecialityCode(), SpecialityModelCodeEnum.air_quality.getCode())){
                // 设置属性名称
                list.stream()
                        .filter(deviceStatusVO -> !ObjectUtils.isEmpty(deviceStatusVO.getPropertyVOList()))
                        .flatMap(deviceStatusVO -> deviceStatusVO.getPropertyVOList().stream())
                        .forEach(propertyVO ->
                                propertyVO.setPropertyName(PropertyCodeUnitEnum.getName(propertyVO.getProperty()))
                        );
            }
            page.setRecords(list);
        }else{
            page.setRecords(new ArrayList<>());
        }
        return page;
    }
}
